﻿using ConfExpert.Forms.Base;
using ConfExpert.Properties;
using ConfExpert.Utility;

using DevExpress.XtraEditors.Repository;
using DevTeam;

using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class Muadiller : XForm
    {
        public Muadiller()
        {
            InitializeComponent();
        }

        private void Muadiller_Load(object sender, EventArgs e)
        {
            gleBrand.Edit.Assign(RI(Rp.Brand));
            ((RepositoryItemGridLookUpEdit)gleBrand.Edit).TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            gleBrand.EditValueChanged += gleBrand_EditValueChanged;

            Rfrsh();

            GridMenu gm = GridManager.GetManager(gridView1).GridMenu;
            gm.AddMenu("Büyük resimler", Zoom_Click, Resources.Zoom_In);

            gridView1.DoubleClick += Zoom_Click;
            splitContainerControl1.AutoSize();
        }

        void gleBrand_EditValueChanged(object sender, EventArgs e)
        {
            Rfrsh();
        }

        BindingSource bs;
        private void Rfrsh()
        {
            bs = gridView1.Init("GetMuadiller", intParse(gleBrand.EditValue));
            gridView1.AddDetailGrid(gridView2, "YeniId,OrgId");
            gridView2.MakeColumnInvisible("YeniId", "OrgId");
            gridView1.Columns["YeniId"].ColumnEdit = RI(Rp.V_Kumas);
            gridView1.FormatAsNumber(1, "TotalQty");
            gridView1.FormatAsNumber(0, "RollsCount");
            gridView2.FormatAsNumber(1, "TotalQty");
            gridView2.FormatAsNumber(0, "RollsCount");
            gridView1.AddSummary("TotalQty", "RollsCount");
            gridView2.AddSummary("TotalQty", "RollsCount");
            gridView1.MakeReadOnly();
            gridView2.MakeReadOnly();

            List<string> Br = new List<string>();
            foreach(DataRow dr in gridView1.Dataset().Tables[2].Rows)
                Br.Add(dr["BrandId"].ToString());

            ((RepositoryItemGridLookUpEdit)gleBrand.Edit).DataSource = new DataView(AppSession.Lookups.Brand, "BrandId in (" + string.Join(",", Br.ToArray()) + ")", "Name", DataViewRowState.OriginalRows);

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
        }


        private void Zoom_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle > -1)
                using (DetailTwinsMuadil dtm = new DetailTwinsMuadil(intParse(bs, "YeniId"), intParse(bs, "OrgId")))
                    dtm.ShowDialog();
        }
    }
}
